package com.sfh.dog.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.TextView;

import com.sfh.dog.R;

public class DogCheckDialog extends Dialog
{

	private String title;
	private GridView gridView;
	private String data[];
	private CheckAdapter mAdapter;
	private SparseArray<String> mCheckArray;
	private IDismiss mDismiss;
	
	public interface IDismiss
	{
		public void close();
	}
	public DogCheckDialog(Context context, String data[], String title,SparseArray<String> checkArray, IDismiss dismiss)
	{
		super(context, R.style.dialog_transparent_notitle);
		this.mDismiss = dismiss;
		this.title = title;
		this.data = data;
		this.mAdapter = new CheckAdapter(context);
		this.mCheckArray = checkArray;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dog_check_view);
		findViewById(R.id.ivClose).setOnClickListener(
				new View.OnClickListener()
				{

					@Override
					public void onClick(View v)
					{
						dismiss();

					}
				});
		((TextView) findViewById(R.id.tvTitle)).setText(title);
		gridView = ((GridView) findViewById(R.id.gvCheck));
		gridView.setAdapter(mAdapter);
		setCanceledOnTouchOutside(true);
	}

	class CheckAdapter extends BaseAdapter
	{
		LayoutInflater mInflater;
		
		CheckAdapter(Context context)
		{
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount()
		{
			return data.length;
		}

		@Override
		public Object getItem(int position)
		{
			return data[position];
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			CheckBox checkBox;

			if (convertView == null)
			{
				convertView = mInflater.inflate(R.layout.check_item, parent,
						false);

			}
			checkBox = (CheckBox) convertView;
			checkBox.setChecked(false);
			
			if (null != mCheckArray.get(getItem(position).hashCode()))
			{
				checkBox.setChecked(true);
			}
			checkBox.setTag(getItem(position));
			checkBox.setText(String.valueOf(getItem(position)));
			checkBox.setOnClickListener(onClickListener);
			return convertView;
		}
		private View.OnClickListener onClickListener = new View.OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				if (null != mCheckArray.get(v.getTag().hashCode()))
				{
					mCheckArray.remove(v.getTag().hashCode());
				}else
				{
					mCheckArray.put(v.getTag().hashCode(), String.valueOf(v.getTag()));
				}
				notifyDataSetChanged();
			}
		};
	}
	
	@Override
	public void dismiss()
	{
		super.dismiss();
		if (mDismiss != null)
		{
			mDismiss.close();
			
			/*StringBuffer stringBuffer = new StringBuffer();
			int size = mCheckArray.size();
			for (int i=0; i <size; i++)
			{
				stringBuffer.append(mCheckArray.valueAt(i)).append(",");
			}
			if (size > 0)
			{
				mDismiss.close(stringBuffer.toString().substring(0, stringBuffer.length() -1));
			}else
			{
				mDismiss.close("");
			}*/
		}

	}
}
